import {
  screen,
  Component,
  ImageAsset,
  Node,
  Sprite,
  SpriteFrame,
  Texture2D,
  UITransform,
  log
} from "cc"

export const fitContain = (target: ImageAsset | Texture2D | Sprite | SpriteFrame | Node, component: Component) => {
  if (target instanceof ImageAsset) {
    const { width, height } = screen.windowSize
    const ratio = target.width / target.height
    const bool = width / height > ratio

    const transform = component.getComponent(UITransform)
    log('*屏幕宽高比*', width / height, '*图片宽高比*', ratio)
    if(bool) {
      transform.setContentSize(height * ratio, height)
    } else {
      transform.setContentSize(width, width / ratio)
    }
  }
}

export const fitCover = (target: ImageAsset | Texture2D | Sprite | SpriteFrame | Node, component: Component) => {
  if (target instanceof ImageAsset) {
    const { width, height } = screen.windowSize
    const ratio = target.width / target.height
    const bool = width / height > ratio

    const transform = component.getComponent(UITransform)
    log('*屏幕宽高比*', width / height, '*图片宽高比*', ratio)
    if(bool) {
      transform.setContentSize(width, width / ratio)
    } else {
      transform.setContentSize(height * ratio, height)
    }
  }
}
